package com.zmn.oms.third.youshu;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.pay.common.constant.PayConsts;

/**
 * 类描述：腾讯有数订单常量定义
 *
 * @author ligang
 * @date 2022/05/06 10:53
 */
public class YouShuOrderConsts {

    /**
     * 预售订单_预付
     */
    public static final int ORDER_TYPE_PREPAID = 4;

    /**
     * 预售订单_尾款
     */
    public static final int ORDER_TYPE_FINAL_PAYMENT = 5;

    /**
     * 待支付
     */
    public static final String ORDER_STATUS_UNPAID = "1110";

    /**
     * 未支付取消
     */
    public static final String ORDER_STATUS_UNPAID_CANCEL = "1130";

    /**
     * 已支付未发货取消
     */
    public static final String ORDER_STATUS_PAID_CANCEL = "1140";

    /**
     * 已支付待发货/已支付待核销
     */
    public static final String ORDER_STATUS_PAID_DELIVERY = "1150";

    /**
     * 已发货
     */
    public static final String ORDER_STATUS_SHIPPED = "1160";

    /**
     * 拒收退货
     */
    public static final String ORDER_STATUS_REJECT_RETURN = "1170";

    /**
     * 销售完成/已收货/已核销
     */
    public static final String ORDER_STATUS_COMPLETE = "1180";

    /**
     * 退款中
     */
    public static final String ORDER_STATUS_REFUNDING = "1280";

    /**
     * 退款完成
     */
    public static final String ORDER_STATUS_REFUND_COMPLETE = "1290";

    /**
     * 映射有数的订单状态
     *
     * @param status
     * @param orderType
     * @param payStatus
     * @return
     */
    public static String getYouShuOrderStatus(int status, int orderType, int payStatus, int resultStatus) {
        boolean isDoing = resultStatus == OrderStatusConsts.WORK_RESULT_DOING;
        boolean isResult = resultStatus == OrderStatusConsts.WORK_RESULT_SUCCESS ||
                resultStatus == OrderStatusConsts.WORK_RESULT_FAIL;
        if (orderType == OrderConsts.ORDER_TYPE_NEW) {
            if (payStatus == PayConsts.PAY_STATUS_NONE) {
                if (status <= OrderStatusConsts.WORK_STATUS_DISTRIBUTE && isDoing) {
                    // 待支付
                    return YouShuOrderConsts.ORDER_STATUS_UNPAID;
                } else if (status < OrderStatusConsts.WORK_STATUS_COMPLETE &&
                        (resultStatus == OrderStatusConsts.WORK_RESULT_DISCARD ||
                                resultStatus == OrderStatusConsts.WORK_RESULT_FAIL)) {
                    // 未支付取消
                    return YouShuOrderConsts.ORDER_STATUS_UNPAID_CANCEL;
                } else if ((status == OrderStatusConsts.WORK_STATUS_TAKE ||
                        status == OrderStatusConsts.WORK_STATUS_VISIT) && isDoing) {
                    // 已支付待发货/已支付待核销
                    return YouShuOrderConsts.ORDER_STATUS_PAID_DELIVERY;
                }
            }
            if (payStatus == PayConsts.PAY_STATUS_DONE) {
                if (status >= OrderStatusConsts.WORK_STATUS_COMPLETE && isResult) {
                    // 销售完成/已收货/已核销
                    return YouShuOrderConsts.ORDER_STATUS_COMPLETE;
                }
            }
        }
        if (orderType == OrderConsts.ORDER_TYPE_REFUND) {
            if (status <= OrderStatusConsts.WORK_STATUS_COMPLETE && isDoing) {
                // 退款中
                return YouShuOrderConsts.ORDER_STATUS_REFUNDING;
            }
            if (status >= OrderStatusConsts.WORK_STATUS_COMPLETE && isResult
                    && payStatus == PayConsts.REFUND_STATUS_DONE) {
                // 退货退款完成
                return YouShuOrderConsts.ORDER_STATUS_REFUND_COMPLETE;
            }
        }
        return "";
    }
}
